package com.foreveross.action;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foreveross.action.weixin.ReservationController;
import com.foreveross.common.CommonUtil;
import com.foreveross.service.ReservationService;
import com.foreveross.vo.HouseUser;
import com.foreveross.vo.HrCode;
import com.foreveross.vo.OrderService;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;

@Controller
@RequestMapping(value = "/sysReservationController")
public class SysReservationController  extends BaseCostroller {
	
	private Logger log = Logger.getLogger(SysReservationController.class);
	@Resource
	private ReservationService reservationService;
	
	/**
	 * 预约服务添加修改
	 * 
	 * @param request
	 * @param response
	 * @param express
	 * @param updateType
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/addOrUpdateReservation")
	public void addOrUpdateReservation(HttpServletRequest request,
			HttpServletResponse response, OrderService orderService,
			String updateType,String userId) {
		ResultVo resultVo = new ResultVo();
		try {
			reservationService.saveOrUpdate(orderService, updateType);
			resultVo.setSuccess(true);
			log.info("预约服务 " + updateType + "成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("预约服务 " + updateType + "失败");
			log.error("预约服务" + updateType + "失败!", e);
		}

		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	
	/**
	 * 查看预约服务
	 * 
	 * @param request
	 * @param response
	 * @param id
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/findOrderService")
	public void findOrderService(HttpServletRequest request,
			HttpServletResponse response, String id) {
		ResultVo resultVo = new ResultVo();
		try {
			 OrderService orderService = (OrderService) this.findObject(
					 OrderService.class, id);
			resultVo.setSuccess(true);
			resultVo.setData(orderService);
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setSuccess(false);
			resultVo.setMessage("查看角色信息失败");
		}
		List list = Arrays.asList("orderType","houseUser","messageHouses","newsHouses","optimizedHouses");
		CommonUtil.PrintWriterResultWithJSON(response, resultVo, list);
	}

	/**
	 * 删除预约服务
	 * 
	 * @param request
	 * @param response
	 * @param id
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/deleteReservation")
	public void deleteReservation(HttpServletRequest request,
			HttpServletResponse response, String ids) {
		ResultVo resultVo = new ResultVo();
		try {
			reservationService.deleteObjecList("OrderService", ids); 
			resultVo.setSuccess(true);
			log.info("预约服务 删除成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("预约服务 删除失败");
			log.info("预约服务 删除失败!");
			e.printStackTrace();
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);

	}
	
	/**
	 * 预约服务列表查询
	 * 
	 * @param request
	 * @param response
	 * @param currentPage
	 * @param rows
	 * @param name
	 * @param type
	 * @param isweixin
	 * @param phone
	 * @param status
	 * @param houseId
	 */

	@RequestMapping(method = RequestMethod.POST, value = "/getReservationList")
	public void getReservationList(HttpServletRequest request,
			HttpServletResponse response,String page,String rows,String name,
			String type,String isweixin,String phone,String status,String houseId,
			String resCode,String userId) {
		ResultVo resultVo = new ResultVo();
		try {
			String weixinId =CommonUtil.getWeixinID(request);
			int countRecord = (int)reservationService.findReservationListCount(weixinId, isweixin, name, type, phone, houseId, status, userId,resCode);
			PageVo<OrderService> pageObj = new PageVo<OrderService>(page, countRecord,rows);
			pageObj.setRows(reservationService.findReservationList(weixinId, pageObj.getpageIndex(), pageObj.getpageSize(), resultVo, isweixin, name, type, phone, houseId, status, userId,resCode));
			resultVo.setData(pageObj);
			resultVo.setSuccess(true);
			log.info("获取预约服务列表 查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("获取预约服务列表 查看失败");
			log.error("获取预约服务列表  查看失败!", e);
		}
		List list = Arrays.asList("orderType","houseUser","messageHouses","newsHouses","optimizedHouses");
		CommonUtil.PrintWriterResultWithJSON(response, resultVo, list);
	}

}
